package com.pansoft.kline.base;

import android.os.Bundle;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pansoft.openplanet.manager.CurrencyAndBourseManager;

/**
 * @author : zzj
 * @e-mail : zhangzhijun@pansoft.com
 * @date : 2018/11/7 20:14
 * @desc :
 * @version: 1.0
 */
public abstract class KlineBaseFragment extends com.efounder.frame.baseui.BaseFragment {

    protected static String TAG = "";
    protected View mRoot;
//    protected Unbinder mRootUnBinder;
    // 标示是否第一次初始化数据
    protected boolean mIsFirstInitData = true;

    //期望的货币单位（默认显示RMB）
    protected String wantCurrency = "CNY";

    public void setWantCurrency(String wantCurrency) {
        this.wantCurrency = wantCurrency;
        refreshPage();
    }

    public abstract void refreshPage();

    public abstract void requestData();

    @Override
    public void onAttach(android.app.Activity activity) {
        super.onAttach(activity);

        TAG = this.getClass().getSimpleName();
        // 初始化参数
        initArgs(getArguments());
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRoot == null) {
            int layId = getContentLayoutId();
            // 初始化当前的跟布局，但是不在创建时就添加到container里边
            View root = inflater.inflate(layId, container, false);
            initWidget(root);
//            BusManager.getBus().register(this);
            mRoot = root;
        } else {
            if (mRoot.getParent() != null) {
                // 把当前Root从其父控件中移除
                ((ViewGroup) mRoot.getParent()).removeView(mRoot);
            }
        }

        return mRoot;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (mIsFirstInitData) {
            // 触发一次以后就不会触发
            mIsFirstInitData = false;
            // 触发
            onFirstInit();
        }

        // 当View创建完成后初始化数据
        initData();
    }


    /**
     * 初始化相关参数
     */
    protected void initArgs(Bundle bundle) {
        wantCurrency = CurrencyAndBourseManager.getCurrencyUnitCode();
    }

    /**
     * 得到当前界面的资源文件Id
     *
     * @return 资源文件Id
     */
    @LayoutRes
    protected abstract int getContentLayoutId();

    /**
     * 初始化控件
     */
    protected void initWidget(View root) {
//        mRootUnBinder = ButterKnife.bind(this, root);
        initAdapter();
        initListener();
    }

    protected void initListener() {

    }

    protected void initAdapter() {

    }

    /**
     * 初始化数据
     */
    protected void initData() {

    }

    /**
     * 当首次初始化数据的时候会调用的方法
     */
    protected void onFirstInit() {

    }

    /**
     * 返回按键触发时调用
     *
     * @return 返回True代表我已处理返回逻辑，Activity不用自己finish。
     * 返回False代表我没有处理逻辑，Activity自己走自己的逻辑
     */
    public boolean onBackPressed() {
        return false;
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
//        BusManager.getBus().unregister(this);
//        RefWatcher refWatcher = App.getRefWatcher(getActivity());
//        refWatcher.watch(this);
    }

}
